package cn.ycc1.baseset.basics.numbers;

import java.util.Calendar;
import java.util.Locale;

/**
 * Numbers
 * @author ycc
 * @date 2025/3/4
 * This section begins with a discussion of the Number class in the java.lang package, its subclasses,
 * and the situations where you would use instantiations of these classes rather than the primitive number types.
 * This section also presents the PrintStream and DecimalFormat classes, which provide methods for writing formatted
 * numerical output.
 *
 * Finally, the Math class in java.lang is discussed. It contains mathematical functions to complement the operators
 * built into the language. This class has methods for the trigonometric functions, exponential functions, and so forth.
 *
 * When working with numbers, most of the time you use the primitive types in your code. For example:
 *
 * There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for
 * each of the primitive data types. These classes "wrap" the primitive in an object. Often, the wrapping is done by the
 * compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you.
 * Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you.
 * For more information, see the section Autoboxing and Unboxing
 *
 * Note: There are four other subclasses of Number that are not discussed here.
 * BigDecimal and BigInteger are used for high-precision calculations.
 * AtomicInteger and AtomicLong are used for multi-threaded applications.
 *
 * There are three reasons that you might use a Number object rather than a primitive:
 *
 * As an argument of a method that expects an object (often used when manipulating collections of numbers).
 * To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of
 * the data type.
 * To use class methods for converting values to and from other primitive types, for converting to and from strings,
 * and for converting between number systems (decimal, octal, hexadecimal, binary).
 * The following table lists the instance methods that all the subclasses of the Number class implement.
 *
 * The following methods convert the value of this Number object to the primitive data type returned.
 *
 * byte byteValue()
 * short shortValue()
 * int intValue()
 * long longValue()
 * float floatValue()
 * double doubleValue()
 * The following methods compare this Number object to the argument.
 *
 * int compareTo(Byte anotherByte)
 * int compareTo(Double anotherDouble)
 * int compareTo(Float anotherFloat)
 * int compareTo(Integer anotherInteger)
 * int compareTo(Long anotherLong)
 * int compareTo(Short anotherShort)
 * boolean equals(Object obj)
 *
 *The method equals(Object obj) determines whether this number object is equal to the argument.
 * The methods return true if the argument is not null and is an object of the same type and with the same numeric value.
 * There are some extra requirements for Double and Float objects that are described in the Java API documentation.
 *
 *
 */
public class Numbers {
    int i = 500;
    float gpa = 3.65f;
    byte mask = 0x7f;

    /**
     * Note: The discussion in this section covers just the basics of the format and printf methods.
     * Further detail can be found in the Basic I/O section of this tutorial, in the "Formatting" page.
     * Using the String.format() to create strings is covered in Strings.
     * @param args
     */
    public static void main(String[] args) {
        long n = 461012;
        System.out.format("%d%n", n);      //  -->  "461012"
        System.out.format("%08d%n", n);    //  -->  "00461012"
        System.out.format("%+8d%n", n);    //  -->  " +461012"
        System.out.format("%,8d%n", n);    // -->  " 461,012"
        System.out.format("%+,8d%n%n", n); //  -->  "+461,012"

        double pi = Math.PI;

        System.out.format("%f%n", pi);       // -->  "3.141593"
        System.out.format("%.3f%n", pi);     // -->  "3.142"
        System.out.format("%10.3f%n", pi);   // -->  "     3.142"
        System.out.format("%-10.3f%n", pi);  // -->  "3.142"
        System.out.format(Locale.FRANCE,
                "%-10.4f%n%n", pi); // -->  "3,1416"

        Calendar c = Calendar.getInstance();
        System.out.format("%tB %te, %tY%n", c, c, c); // -->  "May 29, 2006"

        System.out.format("%tl:%tM %tp%n", c, c, c);  // -->  "2:34 am"

        System.out.format("%tD%n", c);    // -->  "05/29/06"
    }

    /**
     * The following program, BasicMathDemo, illustrates how to use some of these methods:
     * @param args
     */
    public static void main2(String[] args) {
        double a = -191.635;
        double b = 43.74;
        int c = 16, d = 45;

        System.out.printf("The absolute value " + "of %.3f is %.3f%n",
                a, Math.abs(a));

        System.out.printf("The ceiling of " + "%.2f is %.0f%n",
                b, Math.ceil(b));

        System.out.printf("The floor of " + "%.2f is %.0f%n",
                b, Math.floor(b));

        System.out.printf("The rint of %.2f " + "is %.0f%n",
                b, Math.rint(b));

        System.out.printf("The max of %d and " + "%d is %d%n",
                c, d, Math.max(c, d));

        System.out.printf("The min of of %d " + "and %d is %d%n",
                c, d, Math.min(c, d));
    }

    /**
     * The following program, ExponentialDemo, displays the value of e, then calls each of the methods listed in the
     * previous table on arbitrarily chosen numbers:
     */
    public static void main3(String[] args) {
        double x = 11.635;
        double y = 2.76;

        System.out.printf("The value of " + "e is %.4f%n",
                Math.E);

        System.out.printf("exp(%.3f) " + "is %.3f%n",
                x, Math.exp(x));

        System.out.printf("log(%.3f) is " + "%.3f%n",
                x, Math.log(x));

        System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n",
                x, y, Math.pow(x, y));

        System.out.printf("sqrt(%.3f) is " + "%.3f%n",
                x, Math.sqrt(x));
    }

    /**
     * Here is a program, TrigonometricDemo, that uses each of these methods to compute various trigonometric
     * values for a 45-degree angle:
     */
    public static void main4(String[] args) {
        double degrees = 45.0;
        double radians = Math.toRadians(degrees);

        System.out.format("The value of pi " + "is %.4f%n",
                Math.PI);

        System.out.format("The sine of %.1f " + "degrees is %.4f%n",
                degrees, Math.sin(radians));

        System.out.format("The cosine of %.1f " + "degrees is %.4f%n",
                degrees, Math.cos(radians));

        System.out.format("The tangent of %.1f " + "degrees is %.4f%n",
                degrees, Math.tan(radians));

        System.out.format("The arcsine of %.4f " + "is %.4f degrees %n",
                Math.sin(radians),
                Math.toDegrees(Math.asin(Math.sin(radians))));

        System.out.format("The arccosine of %.4f " + "is %.4f degrees %n",
                Math.cos(radians),
                Math.toDegrees(Math.acos(Math.cos(radians))));

        System.out.format("The arctangent of %.4f " + "is %.4f degrees %n",
                Math.tan(radians),
                Math.toDegrees(Math.atan(Math.tan(radians))));
    }

    /**
     * Random Numbers
     * The random() method returns a pseudo-randomly selected number between 0.0 and 1.0.
     * The range includes 0.0 but not 1.0. In other words: 0.0 <= Math.random() < 1.0. To get a number in a different range,
     * you can perform arithmetic on the value returned by the random method. For example,
     * to generate an integer between 0 and 9, you would write:
     *
     * int number = (int)(Math.random() * 10);
     * By multiplying the value by 10, the range of possible values becomes 0.0 <= number < 10.0.
     *
     * Using Math.random works well when you need to generate a single random number. If you need to generate a series of
     * random numbers, you should create an instance of java.util.Random and invoke methods on that object to generate numbers.
     */

}
